{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#简介\" data-toc-modified-id=\"简介-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>简介</a></span></li><li><span><a href=\"#直观理解\" data-toc-modified-id=\"直观理解-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>直观理解</a></span></li><li><span><a href=\"#功能\" data-toc-modified-id=\"功能-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>功能</a></span></li><li><span><a href=\"#其他\" data-toc-modified-id=\"其他-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>其他</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简介\n",
    "1. 自省,定义:为一种可以知道对象是什么,包含何种信息,可以做什么的机制\n",
    "1. 自省的实现:dir()方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 直观理解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**字符串类型数据的自省**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T03:43:04.796139Z",
     "start_time": "2018-05-24T03:43:04.791545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__getnewargs__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mod__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__rmod__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'capitalize',\n",
       " 'casefold',\n",
       " 'center',\n",
       " 'count',\n",
       " 'encode',\n",
       " 'endswith',\n",
       " 'expandtabs',\n",
       " 'find',\n",
       " 'format',\n",
       " 'format_map',\n",
       " 'index',\n",
       " 'isalnum',\n",
       " 'isalpha',\n",
       " 'isdecimal',\n",
       " 'isdigit',\n",
       " 'isidentifier',\n",
       " 'islower',\n",
       " 'isnumeric',\n",
       " 'isprintable',\n",
       " 'isspace',\n",
       " 'istitle',\n",
       " 'isupper',\n",
       " 'join',\n",
       " 'ljust',\n",
       " 'lower',\n",
       " 'lstrip',\n",
       " 'maketrans',\n",
       " 'partition',\n",
       " 'replace',\n",
       " 'rfind',\n",
       " 'rindex',\n",
       " 'rjust',\n",
       " 'rpartition',\n",
       " 'rsplit',\n",
       " 'rstrip',\n",
       " 'split',\n",
       " 'splitlines',\n",
       " 'startswith',\n",
       " 'strip',\n",
       " 'swapcase',\n",
       " 'title',\n",
       " 'translate',\n",
       " 'upper',\n",
       " 'zfill']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = '王凯旋'\n",
    "dir(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T03:43:23.926780Z",
     "start_time": "2018-05-24T03:43:23.922190Z"
    }
   },
   "source": [
    "**字典类型数据的自省**\n",
    "\n",
    "看不到key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T06:13:24.141963Z",
     "start_time": "2018-05-24T06:13:24.136092Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'fromkeys',\n",
       " 'get',\n",
       " 'items',\n",
       " 'keys',\n",
       " 'pop',\n",
       " 'popitem',\n",
       " 'setdefault',\n",
       " 'update',\n",
       " 'values']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wkx = {\n",
    "    'name':'wangkaixuan',\n",
    "    'age':24\n",
    "}\n",
    "\n",
    "dir(wkx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**列表类型数据的自省**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T06:13:32.964041Z",
     "start_time": "2018-05-24T06:13:32.958610Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__iadd__',\n",
       " '__imul__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'append',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'count',\n",
       " 'extend',\n",
       " 'index',\n",
       " 'insert',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'reverse',\n",
       " 'sort']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wkx = ['wangkaixuan',24]\n",
    "dir(wkx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**函数类型数据的自省**\n",
    "1. 多了一个属性`__annotations__`,保存着函数的解释性信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-24T06:20:47.539891Z",
     "start_time": "2018-05-24T06:20:47.535611Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wangkaixuna\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['__annotations__',\n",
       " '__call__',\n",
       " '__class__',\n",
       " '__closure__',\n",
       " '__code__',\n",
       " '__defaults__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__get__',\n",
       " '__getattribute__',\n",
       " '__globals__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__kwdefaults__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__name__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__qualname__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def p(value):\n",
    "    '''\n",
    "    value:打印的数据\n",
    "    '''\n",
    "    print(value)    \n",
    "\n",
    "p('wangkaixuna')\n",
    "dir(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 功能\n",
    "检查某个对象是否具体某个方法,用来做类型检查"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看函数具有,但是函数实例没有的属性列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T08:39:15.756692Z",
     "start_time": "2018-05-29T08:39:15.751044Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__annotations__\n",
      "__call__\n",
      "__closure__\n",
      "__code__\n",
      "__defaults__\n",
      "__get__\n",
      "__globals__\n",
      "__kwdefaults__\n",
      "__name__\n",
      "__qualname__\n"
     ]
    }
   ],
   "source": [
    "class C:\n",
    "    pass\n",
    "\n",
    "obj = C()\n",
    "\n",
    "def func():\n",
    "    pass\n",
    "\n",
    "# len(dir(func))\n",
    "# len(set(dir(func)))\n",
    "for i in sorted(set(dir(func)) - set(dir(C))):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 其他"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:25:18.951701Z",
     "start_time": "2018-05-29T09:25:18.946846Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'chengangong', 'name_temp': 'wangkaixuan'}\n"
     ]
    }
   ],
   "source": [
    "class C():\n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "        self.name_temp = name\n",
    "    def p(self):\n",
    "        pass\n",
    "    \n",
    "c1 = C('wangkaixuan')\n",
    "c1.__dict__['name'] = 'chengangong'\n",
    "print(c1.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:25:26.263491Z",
     "start_time": "2018-05-29T09:25:26.256615Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hhh'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class C(): \n",
    "    name_cls = 'hhh'\n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "        self.name_temp = self.name_cls\n",
    "    def p(self):\n",
    "        pass\n",
    "    \n",
    "c1 = C('wangkaixuan')\n",
    "c1.__class__.name_cls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:07:54.909724Z",
     "start_time": "2018-05-29T09:07:54.904283Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'wangkaixuan'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:07:55.153824Z",
     "start_time": "2018-05-29T09:07:55.149729Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hhh'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1.name_temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:23:54.664534Z",
     "start_time": "2018-05-29T09:23:54.661740Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1.__class__.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:53:13.250752Z",
     "start_time": "2018-05-29T09:53:13.246735Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hhh'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(c1)\n",
    "getattr(c1,'name_cls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:54:38.524568Z",
     "start_time": "2018-05-29T09:54:38.520633Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'wangkaixuan', 'name_temp': 'hhh'}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vars(c1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-29T09:54:43.380245Z",
     "start_time": "2018-05-29T09:54:43.374398Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mappingproxy({'__module__': '__main__',\n",
       "              'name_cls': 'hhh',\n",
       "              '__init__': <function __main__.C.__init__(self, name)>,\n",
       "              'p': <function __main__.C.p(self)>,\n",
       "              '__dict__': <attribute '__dict__' of 'C' objects>,\n",
       "              '__weakref__': <attribute '__weakref__' of 'C' objects>,\n",
       "              '__doc__': None})"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vars(C)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda all lib",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
